<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/10/23 0023
  Time: 上午 11:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
<link rel="stylesheet" href="/static/plugins/ztree/css/zTreeStyle/zTreeStyle.css">
<link rel="stylesheet" href="/static/css/common.css">
<html>
<head>
    <title>添加角色</title>
</head>
<body>
<div>
    <span class="layui-breadcrumb">
        <a href="/" target="_top">首页</a>
        <a href="/role">角色管理</a>
        <a><cite>添加</cite></a>
    </span>
</div>
<div class="form-style">
    <form class="layui-form" action="/role/save" method="post">
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称</label>
            <div class="layui-input-block">
                <input type="text" name="roleName"   lay-verify="required" placeholder="请输入角色名称" autocomplete="off" class="layui-input">
                <input type="hidden" id="ids" name="ids">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="checkbox" lay-filter="checkbox" name="isabled" value="1" title="启用">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">拥有权限</label>
            <div class="layui-input-block">
                <ul id="tree" class="ztree"></ul>
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="decrib" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item" >
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="formDemo">完成</button>
                <a href="/role"><button type="button" class="layui-btn layui-btn-primary">取消</button></a>
            </div>
        </div>
    </form>
</div>

<script src="/static/plugins/jquery.js"></script>
<script src="/static/plugins/layui/layui.js"></script>
<script src="/static/plugins/ztree/js/jquery.ztree.core.js"></script>
<script src="/static/plugins/ztree/js/jquery.ztree.excheck.js"></script>
<script>
    layui.use(['form','element'], function(){
        var form = layui.form;
        var setting = {
            check: {
                enable: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            }
        };
        $.ajax({
            url:"/role/tree",
            type:"get",
            dataType:"json",
            success:function (data) {
                if (data.code ==1){
                    zNodes = data.data;
                    debugger;
                    $.fn.zTree.init($("#tree"), setting, zNodes);
                }else{
                    console.log(data.message);
                }
            },
            error:function () {

            }
        })

        form.on('submit(formDemo)', function(data){
            var treeObj = $.fn.zTree.getZTreeObj("tree");
            var nodes = treeObj.getCheckedNodes(true);
            var ids = [];
            if (null!=nodes){
                $.each(nodes,function (i, item) {
                    ids.push(item.id);
                })
            }
            if(ids.length>0){
                $("#ids").val(ids.join(","));
            }
            return true; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            debugger
        });
    });
</script>
</body>
</html>
